package com.yulongtian.interviewWorks.day20220824;

/**
 * https://leetcode.cn/problems/compress-string-lcci/
 *
 * @author YulongTian
 * @date 2022/8/24 - 10:32
 */
public class InterviewsWork01 {
    public static void main(String[] args) {
        String S = "aabcccccaa";
        System.out.println(S);
        System.out.println(compressString(S));
    }


    public static String compressString(String S) {
        int sLen = S.length();
        if (sLen <= 2) {
            return S;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < sLen; i++) {
            int targetLen = 1;
            if (i == sLen - 1) {
                if (S.charAt(i) == S.charAt(i - 1)) {
                    //stringBuilder.append(targetLen + 1);
                } else {
                    stringBuilder.append(S.charAt(i));
                    stringBuilder.append(1);
                }
                if (sLen < stringBuilder.length()) {
                    return S;
                }
                break;
            } else {
                stringBuilder.append(S.charAt(i));
            }
            int j = i + 1;
            while (S.charAt(i) == S.charAt(j)) {
                targetLen++;
                if (j + 1 < sLen) {
                    j++;
                } else {
                    break;
                }
            }
            i = j - 1;
            stringBuilder.append(targetLen);
            if (sLen < stringBuilder.length()) {
                return S;
            }

        }

        return stringBuilder.toString();

    }

}
